In [1]:
import numpy as np
import pandas as pd
from bokeh import plotting, io

from src.params import DEFAULTFORMAT as FMT
import src.choropleth as choro
In [2]:
FMT.update({'line_width':1, 'line_color':'black'})
choro.empty_map('state', FMT, output='png')
Loading BokehJS ...
Out[2]:
Figure(
id = '1002', …)

Load sample data

In [3]:
cmplx = pd.read_csv('data/test/fitness_county_new.csv', dtype={'fipstext':str})
supp = pd.read_csv('data/test/cbp_suppression_2016_EMP.csv', dtype={'GEO.id2':str})

Divergent data

no customization
In [46]:
p = choro.choropleth_county(supp, 'GEO.id2', 'fips', 'resid', 'divergent', state_outline='None', output='png')
Loading BokehJS ...
In [73]:
fmt = {'background_color':'#d1d1d1', ## add background color
       'line_width':0.5, 'line_color':'#d3d3d3', ## change county line width and color
       'fill_alpha':0.8, ## change opaqueness of color
       'cbar_textfmt':"0.00", 'cbar_max':0.25, 'cbar_min':-0.25} ## change colorbar formatting

p = choro.choropleth_county(supp, 'GEO.id2', 'fips', 'resid', 'divergent', state_outline=None, formatting=fmt, output='svg')
Loading BokehJS ...
Note: cannot export multi-layer (with state_outline) as svg; either export as png or export single layer and empty map to be combined in Illustrator or other svg editing program
change palette, simplify shape, add state outline
In [70]:
fmt = {'st_fill':None, 'line_width':0.5, 'palette':2, 'line_color':'#d3d3d3', 
       'cbar_textfmt':"0.00", 'cbar_max':0.25, 'cbar_min':-0.25, 'fill_alpha':1, 
       'simplify':0.028, ## simplify geo shape
       'background_color':'darkgray'}

p = choro.choropleth_county(supp, 'GEO.id2', 'fips', 'resid', 'divergent', state_outline='after', 
                            formatting=fmt, output='png')
Loading BokehJS ...

show what it would look like with missing data: relies on background being different color from midpoint

In [60]:
supp_missing = supp.copy()
random = np.random.randint(1, len(supp), size=round(0.3*len(supp)))
supp_missing.iloc[random, 7] = np.nan
In [71]:
choro.choropleth_county(supp_missing, 'GEO.id2', 'fips', 'resid', 'divergent', state_outline='after', formatting=fmt, output='png')
Loading BokehJS ...

Sequential data

only MSAs (coded with the constituent counties), no additional formatting
In [15]:
p = choro.choropleth_county(cmplx, 'fipstext', 'fips', 'f_2016', 'sequential', output='png')
Loading BokehJS ...
Add formatting
In [4]:
fmt = {'simplify':0.028, ## simplify geojson shape
       'ncolors':7, 'palette':2, ## specify color palette and number of color breaks
       'wt':1200, 'ht':720, ## dimensions for interactive version (web browser)
       'background_color':None, 'line_color':'white', 'line_width':0.2, ## transparent background and soft white lines
       'st_fill':'#c8c8c8', 'st_alpha':1, 'st_line_width':1, ## color state to be gray and have thick lines
       'cbar_title':'Community Fitness', 'cbar_title_align':'center', ## add color bar title
       'cbar_tick_alpha':0, 'cbar_min':0, 'cbar_max':7, 'cbar_fontsize':"8pt", ## change color bar font, remove ticks
        }

p = choro.choropleth_county(cmplx, 'fipstext', 'fips', 'f_2016', 'sequential', state_outline='both', ## draw state outline before and after
                                formatting=fmt)
Loading BokehJS ...

could also output as bokeh to customize further

In [22]:
p = choro.choropleth_county(cmplx, 'fipstext', 'fips', 'f_2016', 'sequential', state_outline='before',
                                formatting=fmt, output='bokeh')

fmt2 = {'background_color':None, 'st_fill':None, 'st_alpha':0, 'st_line_width':1, 
        'simplify':0.028, 'st_line_color':'black'}
choro.draw_state(p, fmt2)

io.output_notebook()
plotting.show(p)
# io.output_file('cmplx_main.html')
# io.reset_output()
Loading BokehJS ...